<template>
  <div class="tabs">
    <el-tag
        v-for="(item,index) in tags"
        :key="item.path"
        :effect="$route.name === item.name ? 'dark' : 'plain'"
        @click="changeMenu(item)"
        :closable="item.name !== 'firstPage'"
        @close="closeMenu(item,index)"
    >
      {{ item.menuName }}
    </el-tag>
  </div>
</template>

<script>
import {mapState, mapMutations} from "vuex";

export default {
  name: "CommonTabl",
  data() {
    return {}
  },
  created() {
    this.initLoading()
  },
  computed: {
    ...mapState({
      tags: state => state.tab.tabList
    })
  },

  methods: {
    ...mapMutations(['closeTag']),
    initLoading: function () {
      if (this.tags.length === 1) {
        this.$router.push(this.tags[0].path);
      }
    },
    changeMenu: function (item) {
      //点击tab标签
      if (item.path !== this.$router.history.current.fullPath) {
        this.$router.push(item.path)
      }

    },
    closeMenu: function (item, index) {
      //关闭菜单
      this.closeTag(item);

      //动态处理路由
      if (this.$route.name !== item.name) {
        return
      }

      this.$router.push(this.tags[index - 1].path)
    }
  },


}
</script>

<style lang="less">
.tabs {
  position: relative;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 5px 10px #ddd;
  padding-bottom: 5px;
  padding-top: 5px;
  padding-left: 10px;

  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }

}

</style>
